package oli.plugpack.core
{
	import oli.plugpack.services.PlugService;
	
	
	

	public final class PlugIn
	{
		private var _name:String;
		private var _reference:IPlugInActivator;
		private var _dependencies:Vector.<String>;
		private var _dependenciesVersions:Vector.<uint>;
		private var _version:uint;
		private var _description:String;
		private var _services:Vector.<PlugService>;
		private var _vendor:String;
		private var _id:String;
		
		public function PlugIn(plug:IPlugInActivator, meta:XML, services:Vector.<PlugService>)
		{
			//XML.ignoreComments = true;
			//XML.ignoreProcessingInstructions = true;
			//XML.ignoreWhitespace = true;
			
			
			_reference = plug;
			
			_services = services;
			
			_name = meta.name;
			
			// parse version to uint for later comparison
			var v:String = meta.version;
			v = v.replace(".","").replace(".","");
			var vi:uint = uint(v);
			if (vi <= 10)
			{
				vi *= 100;
			}
			else if(vi <100)
			{
				vi*=10;
			}
			_version = vi;
			
			_description = meta.description;
			
			_vendor = meta.vendor;
			
			_dependencies = new Vector.<String>();
			
			_dependenciesVersions = new Vector.<uint>();
			
			_id = meta.id;
			
			var deps:String = meta.dependencies;
			var depes:Array = deps.split(",");
			
			for each(var dep:String in depes)
			{
				var bIndex:Number = dep.indexOf("[");
				var eIndex:Number = dep.indexOf("]");
				if(bIndex != -1 && eIndex!= -1)
				{
					var depver:String = dep.substring(bIndex+1,eIndex);
					var depClean:String = dep.slice(0,bIndex);
					_dependencies.push(depClean);
					depver = depver.replace(".","");
					depver = depver.replace(".","");
					var depverint:uint = uint(depver);
					if (depverint <= 10)
					{
						depverint *= 100;
					}
					else if(depverint <100)
					{
						depverint*=10;
					}
					_dependenciesVersions.push(depverint);
					//trace(deps,dep,depver, dependencies, dependenciesVersions);
				}
				else
				{
					_dependencies.push(dep);
					_dependenciesVersions.push(0);
				}
			}
			
			//TODO register services
		}

		public function get id():String
		{
			return _id;
		}

		public function get vendor():String
		{
			return _vendor;
		}

		public function get services():Vector.<PlugService>
		{
			return _services;
		}

		public function get description():String
		{
			return _description;
		}

		public function get version():uint
		{
			return _version;
		}

		public function get dependenciesVersions():Vector.<uint>
		{
			return _dependenciesVersions;
		}

		public function get dependencies():Vector.<String>
		{
			return _dependencies;
		}

		public function get reference():IPlugInActivator
		{
			return _reference;
		}

		public function get name():String
		{
			return _name;
		}

	}
}